/// @tags: Euclid-like
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const P = 998244353;

ll t, p, q, r, l;

struct Node {
  ll cntu, cntr, sumi, sums, sqrs, prod;

  Node() { cntu = cntr = sumi = sums = sqrs = prod = 0; }

  inline Node operator+(Node const &rhs) const {
    Node c;
    c.cntu = (cntu + rhs.cntu) % P, c.cntr = (cntr + rhs.cntr) % P;
    c.sumi = (sumi + rhs.sumi + cntr * rhs.cntr) % P;
    c.sums = (sums + rhs.sums + cntu * rhs.cntr) % P;
    c.sqrs =
        (sqrs + rhs.sqrs + ((cntu * cntu) % P) * rhs.cntr + (2 * cntu * rhs.sums) % P) %
        P;
    c.prod = ((prod + rhs.prod + ((cntu * cntr) % P) * rhs.cntr) % P + cntu * rhs.sumi +
              cntr * rhs.sums) %
             P;
    return c;
  }
} U, R, ans;

inline Node qpow(Node base, ll k) {
  Node res;
  while (k) {
    if (k & 1) { res = res + base; }
    base = base + base, k >>= 1;
  }
  return res;
}

inline ll calc(ll a, ll b, ll c, ll d) { return ((long double)a * b + c) / d; }

Node solve(ll p, ll q, ll r, ll l, Node const &U, Node const &R) {
  if (!l) return Node();
  if (p >= q) return solve(p % q, q, r, l, U, qpow(U, p / q) + R);
  ll m = calc(l, p, r, q);
  if (!m) return qpow(R, l);
  ll cnt = l - calc(q, m, -r - 1, p);
  return qpow(R, (q - r - 1) / p) + U + solve(q, p, (q - r - 1) % p, m - 1, R, U) +
         qpow(R, cnt);
}

inline int main() {
  cin >> t;
  while (t--) {
    cin >> l >> p >> r >> q;
    U.cntu = 1, U.cntr = 0, U.sumi = 0, U.sums = 0, U.sqrs = 0, U.prod = 0;
    R.cntu = 0, R.cntr = 1, R.sumi = 1, R.sums = 0, R.sqrs = 0, R.prod = 0;
    ans = qpow(U, r / q) + solve(p, q, r % q, l, U, R);
    cout << (ans.sums + r / q) % P << ' '
         << (ans.sqrs + ((r / q) % P) * ((r / q) % P)) % P << ' ' << ans.prod << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("Euclid-like.in", "r", stdin);
  freopen("Euclid-like.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
